package com.dx.mes.service.brm.service;

import static com.dx.mes.common.bean.def.GenericDef.ACTION_FLG_ADD;
import static com.dx.mes.common.bean.def.GenericDef.ACTION_FLG_CANCEL;
import static com.dx.mes.common.bean.def.GenericDef.ACTION_FLG_DELETE;
import static com.dx.mes.common.bean.def.GenericDef.ACTION_FLG_LIST_WHOLE;
import static com.dx.mes.common.bean.def.GenericDef.ACTION_FLG_QUERY;
import static com.dx.mes.common.bean.def.GenericDef.ACTION_FLG_UPDATE;
import static com.dx.mes.common.bean.def.GenericDef.E_FBPCUSLOC_ALREADY_EXIST;
import static com.dx.mes.common.bean.def.GenericDef.E_FBPCUSLOC_IS_NOT_FOUND;
import static com.dx.mes.common.bean.def.GenericDef.E_FBPCUSLOC_OTHER_ERROR;
import static com.dx.mes.common.bean.def.GenericDef.RETURN_CODE_OK;
import static com.dx.mes.common.bean.def.GenericDef.RETURN_CODE_SET_ERROR;
import static com.dx.mes.common.bean.def.GenericDef.RETURN_CODE_UNKNOWN;
import static com.dx.mes.common.bean.def.GenericDef.RETURN_MESG_OK;
import static com.dx.mes.common.bean.def.GenericDef.TRX_OUT;
import static com.dx.mes.common.bean.def.GenericDef.T_FBPBISCUS;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import org.hibernate.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import com.dx.common.table.ppt.Bis_cus_loc;
import com.dx.mes.common.dao.IBisCusDao;
import com.dx.mes.common.transcoder.util.AppContext;
import com.dx.mes.common.transcoder.util.JacksonUtil;
import com.dx.mes.common.transcoder.util.LogUtils;
import com.dx.mes.common.transcoder.util.Util;
import com.dx.mes.facade.brm.service.IFbpbiscusService;
import com.dx.mes.facade.brm.tx.Fbpbiscusloc.FbpbiscuslocI;
import com.dx.mes.facade.brm.tx.Fbpbiscusloc.FbpbiscuslocIA;
import com.dx.mes.facade.brm.tx.Fbpbiscusloc.FbpbiscuslocO;
import com.dx.mes.facade.brm.tx.Fbpbiscusloc.FbpbiscuslocOA;

@Service("fbpbiscusService")
public class FbpbiscusService implements IFbpbiscusService {
	private static final String TX_NAME = T_FBPBISCUS;
	private LogUtils logUtils;

	@Autowired
	private IBisCusDao bisCusDao;
	
	@Override
	public String subMainProc(String evt_no, String strInTrx) {
		AppContext.clear();
		AppContext.setCurrServiceName(FbpbiscusService.class.getSimpleName());
		AppContext.setCurrEventNumber(evt_no);
		logUtils = new LogUtils(FbpbiscusService.class);
		logUtils.info("[InTrx:" + strInTrx + "]");
		
		Session session = bisCusDao.getCrSession();
		FbpbiscuslocO outTrx = new FbpbiscuslocO();
		String strOutTrx = null;
		
		outTrx.setTrx_id(TX_NAME);
		outTrx.setType_id(TRX_OUT);
		outTrx.setRtn_code(String.valueOf(RETURN_CODE_SET_ERROR));
		
		//输入对象
		try {
		FbpbiscuslocI inTrx = new FbpbiscuslocI();
		//将json字符串转换成对象
		inTrx = JacksonUtil.fromJson(strInTrx, FbpbiscuslocI.class);
		char action_flg = inTrx.getAction_flg().charAt(0);
		
		switch (action_flg) {
			case ACTION_FLG_QUERY:
				queryByCondition(inTrx,outTrx);
					break;
			case ACTION_FLG_ADD:
				addFnc(inTrx,outTrx);
					break;
			case ACTION_FLG_UPDATE:
				updateFnc(inTrx,outTrx);
					break;
			case ACTION_FLG_DELETE:
				deleteFnc(inTrx,outTrx);
					break;
			case ACTION_FLG_CANCEL:
				//findMdlbycus(inTrx,outTrx);
				break;
			case ACTION_FLG_LIST_WHOLE:
				listAllFnc(inTrx,outTrx);
				break;
			default:
				errActionFnc(outTrx);
			}
			if (RETURN_CODE_OK.equals(outTrx.getRtn_code())) {
				session.flush();
			} else {
				TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			}
		}catch (Exception ex) {
			outTrx.setRtn_code(RETURN_CODE_UNKNOWN);
			outTrx.setRtn_mesg(ex.toString());
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
		} finally {
			//将一个 Object 或者List对象转化为JSOObject或者JSONArray
			strOutTrx = JacksonUtil.toJSONStr(outTrx);
			logUtils.info("[OutTrx:" + strOutTrx + "]");
		}
		return strOutTrx;
	}
	
	private void listAllFnc(FbpbiscuslocI inTrx, FbpbiscuslocO outTrx) {
		List<FbpbiscuslocOA> oaryList = new ArrayList<FbpbiscuslocOA>();
		String hql = "FROM Bis_cus_loc where 1=1 ";
		List<Bis_cus_loc> bisCusLocList = bisCusDao.find(hql);
		for (Bis_cus_loc bis_cus_loc : bisCusLocList) {
			FbpbiscuslocOA oary = new FbpbiscuslocOA();
			oary.setCus_id(bis_cus_loc.getCus_id());
			oary.setCus_name(bis_cus_loc.getCus_name());
			oary.setCus_desc(bis_cus_loc.getCus_desc());
			oary.setCus_typ(bis_cus_loc.getCus_typ());
			oary.setLoc_info(bis_cus_loc.getLoc_info());
			oaryList.add(oary);
		}
		outTrx.setOary(oaryList);;
		outTrx.setTbl_cnt(oaryList.size());
		outTrx.setRtn_code(	RETURN_CODE_OK);
		outTrx.setRtn_mesg(RETURN_MESG_OK);
		
	}

	//查询对话框根据cus_id查询
	public void queryByCondition(FbpbiscuslocI inTrx,FbpbiscuslocO outTrx){
		try{
		FbpbiscuslocIA iary=inTrx.getIary().get(0);
		String cusId=iary.getCus_id();
		String cusTyp = iary.getCus_typ();
//		String sql = "";
		
		List<FbpbiscuslocOA> oaryList = new ArrayList<FbpbiscuslocOA>();
		String hql = "FROM Bis_cus_loc where 1=1 ";
		if (cusId != null) {
			if (cusId.indexOf("*") == -1) {// 如果不包含*,精确查询
				hql = hql + " and cus_id='" + cusId + "'";
			} else {// 模糊查询
				hql = hql + " and cus_id like '" + cusId.replace("*", "%") + "'";
			}
		}
		if (null != cusTyp) {
				hql = hql + " and cus_typ='" + cusTyp + "'";
		}
		

		//herbinate查询出一个集合
		List<Bis_cus_loc> bisCusLocList = bisCusDao.find(hql);
		for(Bis_cus_loc bis_cus_loc:bisCusLocList){
			FbpbiscuslocOA oary = new FbpbiscuslocOA();
			oary.setCus_id(bis_cus_loc.getCus_id());
			oary.setCus_name(bis_cus_loc.getCus_name());
			oary.setCus_desc(bis_cus_loc.getCus_desc());
			oary.setCus_typ(bis_cus_loc.getCus_typ());
			oary.setLoc_info(bis_cus_loc.getLoc_info());
			oaryList.add(oary);
		}
			/*outTrx.setTbl_cnt(oaryList.size());*/
			outTrx.setOary(oaryList);
			outTrx.setTbl_cnt(oaryList.size());
		}catch (Exception ex) {
			logUtils.error("[" + Util.stackTraceToString(ex) + "]");
			outTrx.setRtn_code(E_FBPCUSLOC_OTHER_ERROR + "");
			outTrx.setRtn_mesg(ex.toString());
			return;
		}
		outTrx.setRtn_code(RETURN_CODE_OK);
		outTrx.setRtn_mesg("success");
		
	}
	
	//删除操作
	public void deleteFnc(FbpbiscuslocI inTrx,FbpbiscuslocO outTrx){
		FbpbiscuslocIA iary=inTrx.getIary().get(0);
		String cusId=iary.getCus_id();

		Bis_cus_loc bis_cus_loc=bisCusDao.get(cusId);
		if(bis_cus_loc ==null){
			outTrx.setRtn_code(E_FBPCUSLOC_IS_NOT_FOUND + "");
			outTrx.setRtn_mesg("not found record "+cusId);
			return;
		}
		bisCusDao.delete(bis_cus_loc);
		
		outTrx.setRtn_code(RETURN_CODE_OK);
		outTrx.setRtn_mesg("success");
		
	}

	//新增操作
	public void addFnc(FbpbiscuslocI inTrx,FbpbiscuslocO outTrx){
		FbpbiscuslocIA iary=inTrx.getIary().get(0);
		String cus_desc=iary.getCus_desc();
		String cus_id_fk=iary.getCus_id();
		String cus_name=iary.getCus_name();
		String cus_typ=iary.getCus_typ();
		String loc_info=iary.getLoc_info();
		
		//String unq_seq_id=iary.getUnq_seq_id();
		Timestamp current_timestamp = Util.getCurrentTimestamp();
		//判断有没有相同cusid的客户
		Bis_cus_loc query_cus_loc=bisCusDao.get(cus_id_fk);
		if(query_cus_loc != null){
			outTrx.setRtn_code(E_FBPCUSLOC_ALREADY_EXIST + "");
			outTrx.setRtn_mesg("客户代码["+cus_id_fk+"]已经存在，请确认！");
			return; 
		}
		//t填充对象
		Bis_cus_loc bis_cus_loc = new Bis_cus_loc();
		if(cus_id_fk != null && cus_id_fk.length() != 0 ){
			bis_cus_loc.setCus_id(cus_id_fk);
		}else{
			outTrx.setRtn_mesg("客户代码为空，请重新输入");
			return;
		}
		
		if(cus_desc != null && cus_desc.length() != 0 ){
			bis_cus_loc.setCus_desc(cus_desc);
		}else {
			outTrx.setRtn_mesg("客户描述为空，请重新输入");
			return;
		}
		
		if(cus_name !=null && cus_name.length() !=0){
			bis_cus_loc.setCus_name(cus_name);
		}else{
			outTrx.setRtn_mesg("客户名称为空，请重新输入");
			return;
		}
		
		if(loc_info !=null && loc_info.length() !=0){
			bis_cus_loc.setLoc_info(loc_info);
		}else{
			outTrx.setRtn_mesg("客户地址信息为空，请重新输入");
			return;
		}
		bis_cus_loc.setCus_typ(cus_typ);
		bis_cus_loc.setEvt_usr(inTrx.getEvt_usr());
		bis_cus_loc.setEvt_timestamp(current_timestamp);
		//执行保存操作
		bisCusDao.save(bis_cus_loc);
		outTrx.setRtn_code(RETURN_CODE_OK);
		outTrx.setRtn_mesg(RETURN_MESG_OK);
		return;
	}
		
	//修改操作
	public void updateFnc(FbpbiscuslocI inTrx,FbpbiscuslocO outTrx){
		FbpbiscuslocIA iary=inTrx.getIary().get(0);
		String cusId=iary.getCus_id();
		//根据ID获得当前对象
		Bis_cus_loc bis_cus_loc=bisCusDao.get(cusId);
		Timestamp current_timestamp = Util.getCurrentTimestamp();
		String cusName =iary.getCus_name();
		String cusDesc =iary.getCus_desc();
		String cusTyp = iary.getCus_typ();
		String locInfo = iary.getLoc_info();
		if(cusId!=null&& cusId.length() != 0){
			bis_cus_loc.setCus_id(cusId);
		}
		if(cusName!=null && cusName.length() !=0){
			bis_cus_loc.setCus_name(cusName);
		}
		if(cusDesc !=null && cusDesc.length() !=0){
			bis_cus_loc.setCus_desc(cusDesc);
		}
		if(cusTyp !=null && cusTyp.length() !=0){
			bis_cus_loc.setCus_typ(cusTyp);
		}
		if(locInfo !=null && locInfo.length() !=0){
			bis_cus_loc.setLoc_info(locInfo);
		}
		bis_cus_loc.setEvt_usr(inTrx.getEvt_usr());
		bis_cus_loc.setEvt_timestamp(current_timestamp);
		//执行修改操作
		bisCusDao.save(bis_cus_loc);
		outTrx.setRtn_code(RETURN_CODE_OK);
		outTrx.setRtn_mesg(RETURN_MESG_OK);
		return;
	}
	
	//错误信息
	public void errActionFnc(FbpbiscuslocO outTrx) {
		outTrx.setRtn_code("0000009");
		outTrx.setRtn_mesg("Invalid Action_flg");
	}
}
